<?php
class iJDebug {
    
    private static $warnings;
    private static $errors;
    
    
    public function __construct() {
        define('TIMESTART', timeMeasure());
    }
    
    public static function log($log_data = '', $rewrite = false) {
      $fh  = fopen(DIR_LOGS . 'iJDebug_log.txt', ($rewrite) ? "w+": "a+");
      fwrite($fh, date('[D M d H:i:s Y]')." ".var_export($log_data,true)."\n");
      fclose($fh);
    }
    
    public static function setWarning($warnings) {
        if(!empty(self::$warnings)) {
             self::$warnings = self::$warnings . ' </br> ';
        }
        self::$warnings = self::$warnings . $warnings;
        
        $nameFile = date('[D M d Y]') . '.txt';
        if(!is_dir(DIR_LOGS))
          mkdir(DIR_LOGS);
        if(!is_dir(DIR_VAR_WARNINGSS))
          mkdir(DIR_VAR_WARNINGSS);
        $fh  = fopen(DIR_VAR_WARNINGSS . $nameFile, "w+");
        fwrite($fh, date('[H:i:s]')." ".var_export(self::$warnings,true)."\n");
        fclose($fh);
        
        self::log(self::$warnings);
    }
    
    public static function setError($errors) {
        if(!empty(self::$errors)) {
             self::$errors = self::$errors . ' </br> ';
        }
        self::$errors = self::$errors . $errors;
        
        $nameFile = date('[D M d Y]') . '.txt';
        if(!is_dir(DIR_LOGS))
          mkdir(DIR_LOGS);
        if(!is_dir(DIR_VAR_ERRORS))
          mkdir(DIR_VAR_ERRORS);
        $fh  = fopen(DIR_VAR_ERRORS . $nameFile, "w+");
        fwrite($fh, date('[H:i:s]')." ".var_export(self::$errors,true)."\n");
        fclose($fh);
        
        self::log(self::$errors);
    }
    
   /* public static function getDebugInfo() {
        $xDataBase = xFactory::singleton('xDataBase');
        $debugDiv = 
        '<div id=\'debug\'> 
            Errors:               ' . self::$errors . ' </br>
            Warnings:             ' . self::$warnings . ' </br>
            MySQL Errors:         ' . $xDataBase->getError() . ' </br>
            Memory In Use:        ' . round(memory_get_usage()/(1024*1024), 2) . ' Mb</br>
            Number Of Querys :    ' . $xDataBase->getNumberOfQuerys() . ' </br>
            Total Execution Time: ' . round(timeMeasure()-TIMESTART, 6) . '
            
        </div>';
        
        return $debugDiv;
    }*/
    
    public static function getDebugInfo() {
        $iJDataBase = iJFactory::singleton('iJDataBase');
        //add Smarty debug indo
        $debug['errors']      = self::$errors;
        $debug['warnings']    = self::$warnings;
        $debug['SQLErrors']   = $iJDataBase->getError();
        $debug['memory']      = round(memory_get_usage()/(1024*1024), 2) . ' Mb';
        $debug['queries']     = $iJDataBase->getNumberOfQuerys();
        $debug['execTime']    = round(timeMeasure()-TIMESTART, 2);
        $debug['queriesTime'] = round($iJDataBase->getQueriesTotalTime(), 2);
        $debug['coreTime']    = $debug['execTime'] - $debug['queriesTime'];
        
        if(!empty(self::$errors) || !empty($debug['SQLErrors'])) {
          $debug['state'] = 'error';
        } else if(!empty(self::$warnings)) {
          $debug['state'] = 'warning';
        } else {
          $debug['state'] = 'fixed';
        }
        
        
        return $debug;
        
    }
}
?>